//
//  HTChoseViewController.m
//  TruckDriver
//
//  Created by Ting Han on 2017/9/30.
//  Copyright © 2017年 Ting Han. All rights reserved.
//

#import "HTChoseViewController.h"
#import "HTChoseCell.h"


@interface HTChoseViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataSource;

@property (nonatomic) NSInteger currentIndex;
@property (nonatomic,strong) NSString *selectedCard;



@end

@implementation HTChoseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupNavi];
    [self setupTable];
    [self readData];
}
- (void)setupNavi{
    UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithTitle:@"保存" style:UIBarButtonItemStyleDone target:self action:@selector(didClickNavRight:)];
    self.navigationItem.rightBarButtonItem = right;
}
- (void)setupTable{
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kHTScreenWidth, kHTScreenHeight - IPHONE_NAVIGATIONBAR_HEIGHT - IPHONE_SAFEBOTTOMAREA_HEIGHT) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = kBackgroundColor;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:@"HTChoseCell" bundle:nil] forCellReuseIdentifier:choseCell];
}
- (void)readData{
    self.currentIndex = - 1;
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Trucks" ofType:@"plist"];
    NSDictionary *dict= [NSDictionary dictionaryWithContentsOfFile:path];
    self.dataSource = [NSMutableArray array];
    switch (self.type) {
        case ChoseTypeCard:
            {
              self.title = @"车牌类型";
              self.dataSource = @[@"黄色车牌",@"蓝色车牌"].mutableCopy;
            }
            break;
        case ChoseTypeLength:
        {
              self.title = @"车长";
              self.dataSource = dict[@"TruckLength"];
        }
            break;
        case ChoseTypeTruck:
        {
            self.title = @"车型";
            self.dataSource = dict[@"TruckType"];
        }
            break;
        default:
            break;
    }
    [self.tableView reloadData];
}

#pragma mark --- tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HTChoseCell *cell = [tableView dequeueReusableCellWithIdentifier:choseCell forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (self.type == ChoseTypeLength) {
        cell.titleStr = [NSString stringWithFormat:@"%@",self.dataSource[indexPath.row]];
    }else{
       cell.titleStr = self.dataSource[indexPath.row];
    }
    cell.isChoosed = NO;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    HTChoseCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (self.currentIndex < 0) {
        cell.isChoosed = !cell.isChoosed;
        self.selectedCard = cell.titleStr;
        
    }else{
        if (self.currentIndex != indexPath.row) {
            HTChoseCell *cellLast = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:self.currentIndex inSection:0]];
            cellLast.isChoosed = NO;
            cell.isChoosed = !cell.isChoosed;
            self.selectedCard = cell.titleStr;
        }
    }
    self.currentIndex = indexPath.row;
}
#pragma mark --- 点击保存
- (void)didClickNavRight:(id)sender{
    if (!self.selectedCard) {
        [HTShowUtil showMessage:@"请选择车牌类型"];
        return;
    }
    if (self.delegate && [self.delegate respondsToSelector:@selector(didChoseItem:withType:)]) {
        [self.delegate didChoseItem:self.selectedCard withType:self.type];
    }
    [self.navigationController popViewControllerAnimated:YES];
}
@end
